library(tidyverse)
library(readxl)
library(fixest)
library(sjPlot)

### SET YOUR WORKING DIRECTORY TO THE FILE WHERE DATA IS STORED

################################### Load data  #################################################

partisan_control_data = read_xlsx('partisan_control_data.xlsx')

descriptive_data = read_xlsx('descriptive_data.xlsx')

bill_data = read_xlsx("bill_data.xlsx")

individual_data = read_xlsx("individual_data.xlsx")

################################### Figure 1  #################################################

### Create figure

ggplot(data = partisan_control_data, aes (x = year, y = perc, group = Control, linetype = Control)) + 
  geom_line() +
  scale_linetype_manual(values = c("Republican" = "dashed", "Democrat" = "solid", "Divided" = "dotted"))+
  theme_minimal() + labs (x = "", y = "State governments (%)") +
  scale_x_discrete(breaks = c("2002", "2004", "2006", "2008", "2010", "2012", "2014", "2016", "2018", "2020"))


#################################### Figure 2 ##################################################

### Obtain values from bill_data

Houses = bill_data %>% filter(chamber == "House")
Senates = bill_data %>% filter(chamber == "Senate")
House_R = bill_data %>% filter(chamber == "House" & majority == "R")
Senate_R = bill_data %>% filter(chamber == "Senate" & majority == "R")
House_D_divided = bill_data %>% filter(chamber == "House" & majority == "D" & unified == 0)
Senate_D_divided = bill_data %>% filter(chamber == "Senate" & majority == "D" & unified == 0)
House_D_unified_veto = bill_data %>% filter(chamber == "House" & majority == "D" & unified == 1 & min_needed_one_chamber == 1)
Senate_D_unified_veto = bill_data %>% filter(chamber == "Senate" & majority == "D" & unified == 1 & min_needed_one_chamber == 1)
House_D_unified_no_veto = bill_data %>% filter(chamber == "House" & majority == "D" & unified == 1 & min_needed_one_chamber == 0)
Senate_D_unified_no_veto = bill_data %>% filter(chamber == "Senate" & majority == "D" & unified == 1 & min_needed_one_chamber == 0)

mean(Houses$party_90)
mean(Senates$party_90)
mean(House_R$party_90)
mean(Senate_R$party_90)
mean(House_D_divided$party_90)
mean(Senate_D_divided$party_90)
mean(House_D_unified_veto$party_90)
mean(Senate_D_unified_veto$party_90)
mean(House_D_unified_no_veto$party_90)
mean(Senate_D_unified_no_veto$party_90)

### Create figure with values from main data

ggplot(data = descriptive_data, aes (x = party_unity_90_10_perc, y = chamber)) + 
  geom_bar(stat = "identity", position = "dodge") +
  theme_minimal() + labs (x = "90% party unity votes (%)", y = "") +
  xlim(0,100) + guides(fill = "none") + facet_wrap(~group)  +
  geom_text(aes(label = sample), vjust = 0.5, hjust = -0.1, color = "black")

############################# Table 2 ######################################

t2_m1 = feols(data = bill_data %>% filter(chamber == "House"),
              fml = as.formula(party_90 ~ democratic + polarization + majority_unity + minority_unity + budgetary_power_score |Year + region),
              cluster = "State") %>% summary()

t2_m2 = feols(data = bill_data %>% filter(chamber == "House"),
              fml = as.formula(party_90 ~ democratic * unified + polarization + majority_unity + minority_unity + budgetary_power_score |Year + region),
              cluster = "State") %>% summary()

t2_m3 = feols(data = bill_data %>% filter(chamber == "House"),
              fml = as.formula(party_90 ~ democratic * unified * no_min_needed_either_chamber + polarization + majority_unity + minority_unity + budgetary_power_score|Year + region),
              cluster = "State")

t2_m4 = feols(data = bill_data %>% filter(chamber == "Senate"),
              fml = as.formula(party_90 ~ democratic + polarization + majority_unity + minority_unity + budgetary_power_score|Year + region),
              cluster = "State")

t2_m5 = feols(data = bill_data %>% filter(chamber == "Senate"),
              fml = as.formula(party_90 ~ democratic * unified + polarization + majority_unity + minority_unity + budgetary_power_score|Year + region),
              cluster = "State")

t2_m6 = feols(data = bill_data %>% filter(chamber == "Senate"),
              fml = as.formula(party_90 ~ democratic * unified * no_min_needed_either_chamber + polarization + majority_unity + minority_unity + budgetary_power_score|Year + region),
              cluster = "State")

etable(t2_m1, t2_m2, t2_m3, t2_m4, t2_m5, t2_m6)

############################ Figure 3 #####################################

### Create data and model compatible with plot_model function

Houses = Houses %>% mutate(Party = ifelse(democratic == 1, "Democratic", "Republican"),
                           Partisan_control = ifelse(unified == 1, "Unified", "Divided"),
                           Veto_proof = ifelse(no_min_needed_either_chamber == 1, "No SM veto", "SM veto"))

housemodel1 = lm(data = Houses,
                 formula = party_90 ~ Partisan_control*Veto_proof*Party + polarization + majority_unity + minority_unity + budgetary_power_score + as.factor(Year) + region)

### Create figure

theme_set(theme_minimal() +
            theme(plot.background = element_rect(fill = "white")))

plot_model(housemodel1, type = "int", title ="", axis.title = c("","Probability of 90% unity vote"),legend.title = "", colors = c("grey10", "grey70"))

################################## Table 3 ########################################################

House_individ_R_minority = individual_data %>% 
  filter(chamber == "House" & party == "R" & majority == "D" & unified == 1) %>% 
  mutate(SM_pivot = ifelse(min_needed_one_chamber == 1, "Republicans control SM pivot", "Republicans do not control SM pivot"))

Senate_individ_R_minority = individual_data %>% 
  filter(chamber == "Senate" & party == "R" & majority == "D" & unified == 1)

t3_m1 = feglm(data = House_individ_R_minority %>% filter(vote_absent == 0), 
              fml = as.formula(vote_yes ~ min_needed_one_chamber + np_score + polarization + majority_unity + minority_unity + budgetary_power_score |Year + region), 
              cluster = "State", family = "binomial") %>% summary()

t3_m2 = feglm(data = House_individ_R_minority %>% filter(vote_absent == 0), 
              fml = as.formula(vote_yes ~ min_needed_one_chamber * np_score + polarization + majority_unity + minority_unity + budgetary_power_score |Year + region), 
              cluster = "State", family = "binomial") %>% summary()

t3_m3 = feglm(data = Senate_individ_R_minority %>% filter(vote_absent == 0), 
              fml = as.formula(vote_yes ~ min_needed_one_chamber + np_score + polarization + majority_unity + minority_unity + budgetary_power_score |Year + region), 
              cluster = "State", family = "binomial")

t3_m4 = feglm(data = Senate_individ_R_minority %>% filter(vote_absent == 0), 
              fml = as.formula(vote_yes ~ min_needed_one_chamber * np_score + polarization + majority_unity + minority_unity + budgetary_power_score |Year + region), 
              cluster = "State", family = "binomial")

etable(t3_m1, t3_m2, t3_m3, t3_m4)

################################### Figure 4 #######################################

### Create model compatible with plot_model()

housemodel_individ = glm(data = House_individ_R_minority,
                         formula = vote_yes ~ SM_pivot * np_score + polarization + majority_unity + minority_unity + budgetary_power_score + as.factor(Year) + region,
                         family = "binomial")

plot_model(housemodel_individ, type = "pred", terms = c("np_score[-1,1]", "SM_pivot"),
           title ="", axis.title = c("Ideology","Probability of Republican yes vote"),legend.title = "", colors = c("grey10", "grey70"))
